package com.sdp.refund.controller;

import java.time.LocalDateTime;
import java.util.Base64;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.Result;
import com.sdp.refund.db.refund_apply.entity.RefundApply;
import com.sdp.refund.db.refund_banner_info.entity.RefundBannerInfo;
import com.sdp.refund.db.refund_banner_info.service.IRefundBannerInfoService;
import com.sdp.refund.db.refund_notice.entity.RefundNotice;
import com.sdp.refund.db.refund_notice.service.IRefundNoticeService;
import com.sdp.refund.db.refund_opinion.entity.RefundOpinion;
import com.sdp.refund.db.refund_opinion.service.IRefundOpinionService;
import com.sdp.refund.db.refund_user.service.IRefundUserService;
import com.sdp.refund.handle.RefundApplyImpl;
import com.sdp.right.control.SysDictController;
import com.sdp.right.utils.RightConstants;

@RestController
@RequestMapping("/MobileController")
public class MobileController {

	@Autowired
	private IRefundUserService iRefundUserService;
	
	@Autowired
	private SysDictController sysDictController;
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Autowired
	private RefundApplyImpl refundApplyImpl;

	@Autowired
	private IRefundBannerInfoService iRefundBannerInfoService;
	
	@Autowired
	private IRefundNoticeService iRefundNoticeService;
	
	@Autowired
	private IRefundOpinionService iRefundOpinionService;
	
	/**
	 * 获取下拉数据源
	 * @param dictCode
	 * @param dictParam
	 * @return
	 */
	@GetMapping(value = "/getDictInfo")
	public Result<?> getDictInfo(@RequestParam(name="dictCode",required=true) String dictCode,@RequestParam(name="dictParam",defaultValue="") String dictParam) {
		return sysDictController.getDictInfo(dictCode, dictParam);
	}
	
	/**
	 * 查询最新5条轮播图信息
	 * @return
	 */
	@GetMapping(value = "/queryTopBannerInfo")
	public Result<?> queryTopBannerInfo() {
		LambdaQueryWrapper<RefundBannerInfo> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(RefundBannerInfo::getIsUse, RightConstants.是.getCode());
		queryWrapper.orderByDesc(RefundBannerInfo::getBannerOrder);
		queryWrapper.last("limit 5");
		return Result.ok(iRefundBannerInfoService.list(queryWrapper));
	}
	/**
	 * 首页的公告信息
	 * @return
	 */
	@GetMapping(value = "/queryRefundNotice")
	public Result<?> queryRefundNotice() {
		LambdaQueryWrapper<RefundNotice> queryWrapper = new LambdaQueryWrapper<>();
		return Result.ok(iRefundNoticeService.getOne(queryWrapper));
	}
	
	
	/**
	 * 根据电话和姓名查询信息
	 * @param obj
	 * @return
	 */
	@PostMapping(value = "/queryRefundUserByTelAndName")
	public Result<?> queryRefundUserByTelAndName(@RequestBody JSONObject obj) throws Exception{
		if(obj==null || StringUtils.isEmpty(obj.getString("parameter"))) {
			return Result.error("用户信息不存在");
		}
		byte[] decodedBytes = Base64.getDecoder().decode(obj.getString("parameter"));
		String parameter = new String(decodedBytes);
		
		JSONObject userObj = JSONObject.parseObject(parameter);
		if(StringUtils.isEmpty(userObj.getString("userTel"))) {
			return Result.error("电话不能为空");
		}else if(StringUtils.isEmpty(userObj.getString("userName"))) {
			return Result.error("姓名不能为空");
		}

		String key = userObj.getString("userTel")+"queryRefundUserByTelAndName";
		if(redisUtil.hasKey(key)) {
			return Result.error("请勿频繁请求");
		}else {
			redisUtil.set(key, key, 5);
		}
		
		
		return Result.ok(iRefundUserService.queryRefundUserByTelAndName(userObj.getString("userName"), userObj.getString("userTel")));
	}
	
	
	/**
	 * 退费申请
	 * @param obj
	 * @return
	 */
	@PostMapping(value = "/refundApply")
	public Result<?> refundApply(@RequestBody JSONObject obj) throws Exception{
		if(obj==null || StringUtils.isEmpty(obj.getString("parameter"))) {
			return Result.error("提交的信息不存在");
		}
		
		//再进行BASE64解码
		byte[] decodedBytes = Base64.getDecoder().decode(obj.getString("parameter"));
		String parameter = new String(decodedBytes);
		RefundApply refundApply = JSONObject.parseObject(parameter, RefundApply.class);
		if(refundApply==null) {
			return Result.error("提交信息不合法");
		}
		if(StringUtils.isEmpty(refundApply.getUserTel()) || StringUtils.isEmpty(refundApply.getIdNum())
				|| StringUtils.isEmpty(refundApply.getBankNum()) || StringUtils.isEmpty(refundApply.getBankAc())
				|| StringUtils.isEmpty(refundApply.getIdCardFrontPhoto()) || StringUtils.isEmpty(refundApply.getIdCardBackPhoto())
				|| StringUtils.isEmpty(refundApply.getBankPhoto()) || StringUtils.isEmpty(refundApply.getUserName())) {
			return Result.error("提交信息不完整");
		}
		
		
		String key = refundApply.getUserTel()+"refundApply";
		if(redisUtil.hasKey(key)) {
			return Result.error("请勿频繁请求");
		}else {
			redisUtil.set(key, key, 5);
		}
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(RefundApply.class, refundApply);
		refundApplyImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok();
		}else {
			return Result.error(businessContext.getMessage());
		}
		
	}
	
	
	/**
	 * 用户建议
	 * @param obj
	 * @return
	 */
	@PostMapping(value = "/opinion")
	public Result<?> opinion(@RequestBody JSONObject obj) throws Exception{
		if(obj==null || StringUtils.isEmpty(obj.getString("parameter"))) {
			return Result.error("提交的信息不存在");
		}
		
		//再进行BASE64解码
		byte[] decodedBytes = Base64.getDecoder().decode(obj.getString("parameter"));
		String parameter = new String(decodedBytes);
		RefundOpinion opinion = JSONObject.parseObject(parameter, RefundOpinion.class);
		if(opinion==null) {
			return Result.error("提交信息不合法");
		}
		if(StringUtils.isEmpty(opinion.getUserTel()) || StringUtils.isEmpty(opinion.getUserName()) || StringUtils.isEmpty(opinion.getOpinionContent())) {
			return Result.error("提交信息不完整");
		}
		
		
		String key = opinion.getUserTel()+"opinion";
		if(redisUtil.hasKey(key)) {
			return Result.error("请勿频繁请求");
		}else {
			redisUtil.set(key, key, 5);
		}
		opinion.setCreateTime(LocalDateTime.now());
		iRefundOpinionService.save(opinion);
		
		return Result.ok();
		
	}
	
}
